package com.suray.wcs.service.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.wcs.service.hk.agv.type.HkAgvProcessType;
import com.suray.wcs.service.hk.agv.type.HkAgvTaskStatus;
import com.suray.wcs.service.system.entity.AgvTaskDB;
import com.suray.wcs.service.system.mapper.AgvTaskDBMapper;
import com.suray.wcs.service.system.service.AgvTaskDBService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author MJ
 * @since 2021-09-27
 */
@Service
public class AgvTaskDBServiceImpl extends ServiceImpl<AgvTaskDBMapper, AgvTaskDB> implements AgvTaskDBService {
    @Override
    public boolean insertTask(AgvTaskDB taskDB) {
        return save(taskDB);
    }

    @Override
    public boolean updateTask(AgvTaskDB taskDB) {
        return updateById(taskDB);
    }

    @Override
    public boolean storeTask(AgvTaskDB taskDB) {
        AgvTaskDB task = getById(taskDB);
        if (task == null) {
            return save(taskDB);
        }

        return updateById(taskDB);
    }

    @Override
    public List<AgvTaskDB> findUnFinishTask() {
        LambdaQueryWrapper<AgvTaskDB> eq = new QueryWrapper<AgvTaskDB>().lambda()
                .ne(AgvTaskDB::getTaskStatus, HkAgvTaskStatus.FINISH.getValue())
                .ne(AgvTaskDB::getTaskPro, HkAgvProcessType.END.getValue())
                .orderByDesc(AgvTaskDB::getPriority).orderByAsc(AgvTaskDB::getTaskId);
        return this.list(eq);
    }

    @Override
    public void deleteTaskForDays(Integer days) {
        baseMapper.deleteTaskForDays(days);
    }
}
